// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var js_platform_main = require("./platform_main.js");
	var css_transportNotice = require("./transportNotice.css");
	var constant = require("constant");
	var tools = require("tools");
	var pageIndex = 1,
		pageSize = 5,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var financeId;
	var caseSizeMap = new tools.Map();
	var recieveNoticeId;
	var financeNumber;
	var totalLoanAmount;
	var fileUuid;
	var loanNoticeId;
	var left;
	var init = function(page) {
		recieveNoticeId = page.params.recieveNoticeId || base.getUrlParam("recieveNoticeId");
		financeId = page.params.financeId || base.getUrlParam("financeId");
		financeNumber = page.params.financeNumber || base.getUrlParam("financeNumber");
		totalLoanAmount = page.params.totalLoanAmount || base.getUrlParam("totalLoanAmount");
		left = page.params.left || base.getUrlParam("left");
		$(".left").removeClass("none");
		leftMenu.init(6);
		
		caseSizeMap.toString = function() {
			var str = "";
			for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
				if (this.container[keys[i]] == 0) {
					continue;
				}
				str = str  + this.container[keys[i]] + "X" + keys[i] + "<br/>";
			}
			return (str == "" ? "箱统计信息：无" : "箱统计信息：" + str);
		}
		
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		$("#main").css("background", "#ffffff");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		inquireWaitTransportNoticeBulkstockInventoryList();
		inquireLogisticsCompanyList();
		$(".financeNumber").html(financeNumber);
		js_platform_main.init(0);
		initEvent();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});
		
		$("#transportNotice .inventoryIdCheckedsAll").unbind("click").click(function() {
			caseSizeMap.put("10GP", 0);
			caseSizeMap.put("20GP", 0);
			caseSizeMap.put("40GP", 0);
			caseSizeMap.put("48GP", 0);
			caseSizeMap.put("10HQ", 0);
			caseSizeMap.put("20HQ", 0);
			caseSizeMap.put("40HQ", 0);
			caseSizeMap.put("48HQ", 0);
			caseSizeMap.put("10RH", 0);
			caseSizeMap.put("20RH", 0);
			caseSizeMap.put("40RH", 0);
			caseSizeMap.put("48RH", 0);
			if ($(this).prop("checked")) {
				$("#transportNotice .inventoryId").prop("checked", true);
			} else {
				$("#transportNotice .inventoryId").prop("checked", false);
			}
			$.each($("#transportNotice .inventoryId:checked"), function(i, val) {
				statisticalCaseSize(caseSizeMap, $.trim($(this).parents("tr").find(".caseSize").html()));
			});
			$(".caseInfo").html(caseSizeMap.toString());
		});

		//点击通知运输列表
		$(".transportNotice_list").unbind("click").click(function() {
			router.route("main/transportNotice_list.html?left=5", true);
		});
		
		//点击理货 详情
		$("#tallyingListBody").on("click", ".tallyingListDetail", function() {
			var inventoryId = [];
			var $this = $(this).parents("tr.tr_style");
			inventoryId.push($this.attr("inventoryId"));
			router.route("financing/tallyDetail.html?inventoryId=" + inventoryId + "&recieveNoticeId=" + recieveNoticeId + "&type=readOnly&financeId=" + financeId, true);

		});
		
		$("#transportNotice .inventoryId").unbind("click").click(function(){
			caseSizeMap.put("10GP", 0);
			caseSizeMap.put("20GP", 0);
			caseSizeMap.put("40GP", 0);
			caseSizeMap.put("48GP", 0);
			caseSizeMap.put("10HQ", 0);
			caseSizeMap.put("20HQ", 0);
			caseSizeMap.put("40HQ", 0);
			caseSizeMap.put("48HQ", 0);
			caseSizeMap.put("10RH", 0);
			caseSizeMap.put("20RH", 0);
			caseSizeMap.put("40RH", 0);
			caseSizeMap.put("48RH", 0);
			if ($("#transportNotice .inventoryId:checked").length != $("#transportNotice .inventoryId").length) {
				$("#transportNotice .inventoryIdCheckedsAll").prop("checked", false);
			} else {
				$("#transportNotice .inventoryIdCheckedsAll").prop("checked", true);
			}
			
			$.each($("#transportNotice .inventoryId:checked"), function(i, val) {
				statisticalCaseSize(caseSizeMap, $.trim($(this).parents("tr").find(".caseSize").html()));
			});
			
			$(".caseInfo").html(caseSizeMap.toString());
		});

		//点击下发通知
		$("#transportNoticeSubmit").on("click", function() {
			createTransportNotice();
		});

		//点击融资编号
		$(".financeNumber").unbind("click").click(function() {
			router.route("financing/financingDetailInfo.html?financeId=" + financeId, true);
		});
	};
	
	var statisticalCaseSize = function(m, c) {
		switch (c){
			case "10GP":
				m.put("10GP", m.get("10GP")*1 + 1);
				break;
			case "20GP":
				m.put("20GP", m.get("20GP")*1 + 1);
				break;
			case "40GP":
				m.put("40GP", m.get("40GP")*1 + 1);
				break;
			case "48GP":
				m.put("48GP", m.get("48GP")*1 + 1);
				break;
			case "10HQ":
				m.put("10HQ", m.get("10HQ")*1 + 1);
				break;
			case "20HQ":
				m.put("20HQ", m.get("20HQ")*1 + 1);
				break;
			case "40HQ":
				m.put("40HQ", m.get("40HQ")*1 + 1);
				break;
			case "48HQ":
				m.put("48HQ", m.get("48HQ")*1 + 1);
				break;
			case "10RH":
				m.put("10RH", m.get("10RH")*1 + 1);
				break;
			case "20RH":
				m.put("20RH", m.get("20RH")*1 + 1);
				break;
			case "40RH":
				m.put("40RH", m.get("40RH")*1 + 1);
				break;
			case "48RH":
				m.put("48RH", m.get("48RH")*1 + 1);
				break;
			default:
				break;
		}
	}

	//创建运输通知
	var createTransportNotice = function() {

		var noticeRemark = $(".noticeRemark").val();
		var inventoryId = [];
		var logisticsCompanyId = $("#logisticsCompanyId").val();
		var logisticsCompanyName = $("#logisticsCompanyId option:selected").text();
		$.each($("#inventoryId:checked"), function(i, val) {
			inventoryId[i] = val.value;
		});
		if (inventoryId.length == 0) {
			base.alert(2, "请选择货箱");
			return;
		}
		base.fnAjax({
			action: "createTransportNotice.json",
			oData: {
				financeId: financeId,
				financeNumber: financeNumber,
				noticeRemark: noticeRemark,
				logisticsCompanyId: logisticsCompanyId,
				logisticsCompanyName: logisticsCompanyName,
				inventoryId: inventoryId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "下发通知成功");
					js_platform_main.init();
					router.route("main/transportNotice_list.html?left=5", true);
				} else {
					base.alert(2, "下发通知失败");
				}
			}
		});
	}
	
	// 融资详情
	var inquireFinanceApplicationDetail = function() {

		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			oData: {
				financeId: financeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#bookingSpaceTpl").html();
					var html = juicer(tpl, data);
					$("#transportNoticeSubmit").before(html);
					$(".receive").append(data.datas[0].deliverProvince+data.datas[0].deliverCity+data.datas[0].deliverDistrict+data.datas[0].deliverAddress);
					$(".delivery").append(data.datas[0].receiveProvince+data.datas[0].receiveCity+data.datas[0].receiveDistrict+data.datas[0].receiveAddress);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	// 查询物流公司列表
	var inquireLogisticsCompanyList = function() {
		base.fnAjax({
			action: "inquireLogisticsCompanyList.json", //接口名称
			oData: {
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					var tpl = $("#companyTpl").html();
					var html = juicer(tpl, data);
					$("#logisticsCompanyId").html(html);
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	//查询理货任务列表
	var inquireWaitTransportNoticeBulkstockInventoryList = function() {
		base.fnAjax({
			action: "inquireWaitTransportNoticeBulkstockInventoryList.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulkstockTpl1").html();
					var html = juicer(tpl, data);
					$("#bulkstockListBody1").html(html);
					inquireFinanceApplicationDetail();
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});